<script setup>
import { ref } from 'vue'
import { artGetListService } from '@/api/article'
import ChannelSelect from './components/ChannelSelect.vue'
import { formatTime } from '@/utils/format'
import { Edit, Delete } from '@element-plus/icons-vue'
import ArticleEdit from './components/ArticleEdit.vue'
const params = ref({
    pagenum: 1,
    pagesize: 5,
    cate_id: '',
    state: ''
})
const loading = ref(false)
//文章列表
const articleList = ref([])
const total = ref(0)
const getArticleList = async () => {
    loading.value = true
    const res = await artGetListService(params.value)
    articleList.value = res.data.data
    total.value = res.data.total
    loading.value = false
}
getArticleList()

const onSizeChange = () => {
    params.value.pagenum = 1
    getArticleList()
}
const onCurrentChange = () => getArticleList()
const onReset = () => {
    params.value.cate_id = ''
    params.value.state = ''
    params.value.pagenum = 1
    getArticleList()
}
//文章ref
const artEditRef = ref()
//添加文章
const addArticle = () => {
    artEditRef.value.open({})
}
//编辑文章
const editArticle = row => {
    artEditRef.value.open(row)
}
//删除文章
//添加成功
const onSuccess = type => {
    getArticleList()
}
const deleteArticle = () => {}
</script>
<template>
    <page-container title="文章管理">
        <template #extra>
            <el-button type="primary" @click="addArticle"> 添加文章 </el-button>
        </template>
        <!-- 表单区域 -->
        <el-form inline>
            <el-form-item label="文章分类：">
                <channel-select v-model="params.cate_id"></channel-select>
            </el-form-item>
            <el-form-item label="发布状态:">
                <el-select v-model="params.state" style="width: 200px">
                    <el-option label="已发布" value="已发布"></el-option>
                    <el-option label="草稿" value="草稿"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="getArticleList">搜索</el-button>
                <el-button @click="onReset">重置</el-button>
            </el-form-item>
        </el-form>
        <!-- 表格区域 -->
        <el-table :data="articleList" style="width: 100%" v-loading="loading">
            <el-table-column label="文章标题" prop="title"></el-table-column>
            <el-table-column label="分类" prop="cate_name"></el-table-column>
            <el-table-column prop="pub_date" label="发表时间">
                <template #default="{ row }">
                    {{ formatTime(row.pub_date) }}
                </template>
            </el-table-column>
            <el-table-column label="状态" prop="state"></el-table-column>
            <el-table-column label="操作">
                <template #default="{ row }">
                    <el-button :icon="Edit" type="primary" circle @click="editArticle(row)"></el-button>
                    <el-button :icon="Delete" type="warning" circle @click="deleteArticle"></el-button>
                </template>
            </el-table-column>
        </el-table>
        <ArticleEdit ref="artEditRef" @success="onSuccess"></ArticleEdit>
        <el-pagination
            v-model:current-page="params.pagenum"
            v-model:page-size="params.pagesize"
            :page-sizes="[2, 3, 4, 5]"
            :size="params.pagesize"
            :background="true"
            layout="jumper, total, sizes, prev, pager, next"
            :total="total"
            @size-change="onSizeChange"
            @current-change="onCurrentChange"
            style="margin-top: 30px; justify-content: flex-end" />
    </page-container>
</template>

<style lang="scss" scoped></style>
